
tag=$1
#mBB_anchor6_HASENERGYLOSSCORRECTION
res=$2
#3000
#saturation_anchor6

echo $tag
echo $res
echo
echo
#compare fromPT and coherent fit result
for ii in $(find 1*MC*/coherent_$tag/Sys -type d )
do 
    fitfile=$(echo $(pwd)/$ii/*.root)
    if [ ! -e $fitfile ] 
    then
        continue
    fi
    
    echo $fitfile
    
    truefile=$(echo $(pwd)/$ii/../../fromPT*.root)
    echo $truefile
    
    hh=$(echo $ii|tr -s '/' '_')
#$(echo $ii | awk -F\/ '{print $1"-"$2}')
    dir=$tag\_checkMCFit/$hh
    echo $dir
    
    mkdir -p $dir
    cd $dir
    ln -s $fitfile $hh$(echo $fitfile |  awk -F\/ '{print $NF}')
    ln -s $truefile $hh$(echo $truefile |  awk -F\/ '{print $NF}')
    
    file1=$(find *out*.root)
    file2=$(find *from*.root)
    echo $file1
    echo $file2
    echo
    
    root -b -l -q '~/.task/2dbetabloch/plotcode/drawSys.C("MC","'$file1'","'$file2'", 0)'
    
    for kk in $(seq 0 3)
    do 
        root -b -l -q '~/.task/2dbetabloch/plotcode/drawSys.C("FIT","'$file1'","'$file2'", 0, '$kk')'
    done
    
    cd -
done > see$tag\_CheckMCFit

cd $tag\_checkMCFit
mkdir -p good bad
mv $(grep ierr ../1*MC*/coherent_$tag/ID$res\_2/see.log | grep "nbeta 100" | grep "ierr 4" | awk -F\/ '{print $2"_"$3"_Sys"}') bad/
mv $(grep strange ../1*MC*/coherent_$tag/ID$res\_2/see.log | awk -F\/ '{print $2"_"$3"_Sys"}') bad/
mv $(grep ierr ../1*MC*/coherent_$tag/ID$res\_2/see.log | grep "success" | grep "inf"|  awk -F\/ '{print $2"_"$3"_Sys"}') bad/
mv $(grep ierr ../1*MC*/coherent_$tag/ID$res\_2/see.log | grep "success" | awk -F\/ '{print $2"_"$3"_Sys"}') good/
mv $(grep ierr ../1*MC*/coherent_$tag/ID$res\_2/see.log | grep "nbeta 100" | grep "ierr 0" | awk -F\/ '{print $2"_"$3"_Sys"}') good/

cd -

#compare LUND and coherent fit result
for ii in $(find 1*DATA*/coherent_$tag/Sys -type d )
do 
    fitfile=$(echo $(pwd)/$ii/*.root)
    if [ ! -e $fitfile ] 
    then
        continue
    fi

    jj=$(echo $ii|tr -s '/' '_')
    dir=$tag\_checkDataFit/$jj
    mkdir -p $dir
    cd $dir;

    echo $fitfile

    file1=$fitfile

    root -b -l -q '~/.task/2dbetabloch/plotcode/drawSys.C("data","'$file1'","", 1)'

    #no electron
    for kk in $(seq 0 3)
    do 
        root -b -l -q '~/.task/2dbetabloch/plotcode/drawSys.C("dataipar","'$file1'","", 1, '$kk')'
    done

    ls;
    cd -;
done > see$tag\_CheckDataFit

grep please see$tag\_CheckDataFit 

cd $tag\_checkDataFit
mkdir -p good bad
mv $(grep ierr ../1*DATA*/coherent_$tag/ID$res\_2/see.log | grep "nbeta 100" | grep "ierr 4" | awk -F\/ '{print $2"_"$3"_Sys"}') bad/
mv $(grep strange ../1*DATA*/coherent_$tag/ID$res\_2/see.log | awk -F\/ '{print $2"_"$3"_Sys"}') bad/
mv $(grep ierr ../1*DATA*/coherent_$tag/ID$res\_2/see.log | grep "success" | grep "inf" | awk -F\/ '{print $2"_"$3"_Sys"}') bad/
mv $(grep ierr ../1*DATA*/coherent_$tag/ID$res\_2/see.log | grep "success" | awk -F\/ '{print $2"_"$3"_Sys"}') good/
mv $(grep ierr ../1*DATA*/coherent_$tag/ID$res\_2/see.log | grep "nbeta 100" | grep "ierr 0" | awk -F\/ '{print $2"_"$3"_Sys"}') good/

cd -
